/**
 * 抽奖九宫格组件
 */
import {
  prizeList,
} from '/common/config';
import {
  timeListener,
} from '/utils';

import roulette from '/utils/roulette';

Component({
  data: {
    prizeList,
    activeIndex: 0,
    status: '',
    nextTime: '', // 下一次活动时间
    showFortunateModal: false, // 恭喜中奖模态框
    showUnfortunateModal: false, // 未中奖模态框
    awardData: {}, // 奖品数据
    nextTimeDate: '',
  },

  props: {
    hide: true,
  },

  didMount() {
    roulette.init(this);
  },

  didUpdate(prevProps) {
    const { hide: prevHide } = prevProps;
    const { hide: nowHide } = this.props;
    if (prevHide && !nowHide) {
      // 当页面显示 onShow
      roulette.init(this);
    }
  },

  didUnmount() {
    // 停止倒计时
    timeListener(null, null, null, true);
  },

  methods: {
    handleOnTapLottery() {
      roulette.eventListener();
    },

    // 展示中奖结果
    handleShowResult(res) {
      if (res) {
        this.setData({
          showFortunateModal: true,
          awardData: res,
        });
      } else {
        this.setData({
          showUnfortunateModal: true,
        });
      }
    },

    // 关闭中奖结果模态框
    onModalClose() {
      this.setData({
        showFortunateModal: false,
        showUnfortunateModal: false,
      });
      // 刷新抽奖状态
      roulette.refresh();
    },
  },
});